
import Vue from '../node_modules/vue/dist/vue.js'
import Vuex from 'vuex'
import app from './app.vue'
import MintUI from 'mint-ui'
import 'mint-ui/lib/style.css'
import "./lib/mui/css/mui.css"
import "./lib/mui/css/icons-extra.css"
import VueRouter from 'vue-router'
import router from './router.js'
import VueResource from 'vue-resource'
import memont from 'moment'
import VuePreview from 'vue-preview'
import axios from 'axios'
import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(MintUI)
Vue.use(VueRouter)
Vue.use(VueResource)
Vue.use(VuePreview)
Vue.use(Vuex)
Vue.use(VideoPlayer)
Vue.prototype.$axios = axios;
Vue.use(ElementUI);




const store = new Vuex.Store({
    state: {
        //将本地存储的数据给shopcar，保证刷新后数据依然存在

        shopcar: JSON.parse(localStorage.getItem('shopcar') || '[]'),
        Leavemessage: JSON.parse(localStorage.getItem('messages') || '[]')

    },
    mutations: {
        // 将商品加入到购物车
        AddtoshopCar(state, goodinfo) {
            var flag = false;
            state.shopcar.some(item => {
                if (item.id == goodinfo.id) {
                    item.number += parseInt(goodinfo.number);
                    flag = true;
                }
                return true;
            });
            if (flag == false) {

                state.shopcar.push(goodinfo);

            }
            localStorage.setItem('shopcar', JSON.stringify(state.shopcar));

        },
        // 修改商品数量
        shopcarAdd(state, shopcarinfo) {
            state.shopcar.forEach(item => {
                if (item.id == shopcarinfo.id) {
                    item.number = parseInt(shopcarinfo.count);
                }
            })
            localStorage.setItem('shopcar', JSON.stringify(state.shopcar));
        },
        // 删除购物车中的商品
        removeshop(state, id) {
            state.shopcar.forEach((item, i) => {
                if (item.id == id) {
                    state.shopcar.splice(i, 1);
                }
            })
            localStorage.setItem('shopcar', JSON.stringify(state.shopcar));
        },
        // 更新选中状态判断是否需要购买
        updateselect(state, info) {
            state.shopcar.some(item => {
                if (item.id == info.id) {
                    item.select = info.value;
                    return true
                }
            })
            localStorage.setItem('shopcar', JSON.stringify(state.shopcar));
        },
        // 移除结算完的商品
        removepaid(state) {

            state.shopcar.forEach((item, i) => {
                if (item.select) {

                    state.shopcar.splice(i, 1);

                }
            })
            localStorage.setItem('shopcar', JSON.stringify(state.shopcar));

        },
        // 将用户提交反馈存到本地
        addtomessages(state, messageinfo) {
            state.Leavemessage.push(messageinfo);
            localStorage.setItem('messages', JSON.stringify(state.Leavemessage));
        }
    },
    getters: {
        // 获取所有商品数量
        getAll(state) {
            var allgoodnum = 0;
            state.shopcar.forEach(item => {
                allgoodnum += parseInt(item.number)

            });
            return allgoodnum;
        },
        // 获取指定id商品的数量
        getNum(state) {
            var num = {}
            state.shopcar.forEach(item => {
                num[item.id] = item.number;
            })
            return num;
        },
        // 获取指定id商品的选中状态
        getselect(state) {
            var select = {}
            state.shopcar.forEach(item => {
                select[item.id] = item.select;
            })
            return select;
        },
        // 获取购物车中需要购买的商品总价及数量
        getallPrice(state) {
            var price = { count: 0, pay: 0 }
            state.shopcar.forEach(item => {
                if (item.select) {
                    price.count += item.number;
                    price.pay += item.price * item.number;
                }
            })
            return price;
        }

    }
})


Vue.http.options.emulateJSON = true;


Vue.filter('dataFormat', function (dataStr, pattern = "YYYY-MM-DD HH:mm:ss") {
    return memont(dataStr).format(pattern);
})

var vm = new Vue({
    el: '#app',
    data: {
        msg: ''
    },
    methods: {},
    // 简易写法
    render: c => c(app),

    router: router,
    store: store
})

